package edu.cs346.group5.ui.loginpages;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Date;

import javax.swing.JTextField;
import javax.swing.JPasswordField;

import edu.cs346.group5.logic.Login;
import edu.cs346.group5.ui.userpages.home;

import net.miginfocom.swing.MigLayout;


public class login extends JFrame {

	private JPanel contentPane;
	private JTextField txtUsername;
	private JPasswordField passwordField;
	private JButton btnForgotPassemail;
	private JButton btnRegister;
	private JButton btnLogin;

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public login() {
		setTitle("Login");
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[86px][89px][147px]", "[23px][][][][][4px][23px][23px]"));
		
		txtUsername = new JTextField();
		txtUsername.setText("Username");
		contentPane.add(txtUsername, "cell 0 0 3 1,growx,aligny center");
		txtUsername.setColumns(10);
		
		txtUsername.addKeyListener( new KeyAdapter() {
	        public void keyPressed(KeyEvent ke) {
	        	if(ke.getKeyCode() == KeyEvent.VK_ENTER)
	        	{
	        		Boolean check = Login.CheckPassword(txtUsername.getText(), passwordField.getText());
					
					if(check == true){
						if(txtUsername.getText().equals("admin"))
							JOptionPane.showMessageDialog(null, "Welcome back, administrator.", "Notice", JOptionPane.INFORMATION_MESSAGE);
						home x = new home();
						x.setVisible(true);
						dispose();
					}
					else{
						JOptionPane.showMessageDialog(null, "Invalid login Information.", "Notice", JOptionPane.INFORMATION_MESSAGE);
					}
	        	}

	        }
		});
	
		
		passwordField = new JPasswordField("password");
		contentPane.add(passwordField, "cell 0 1 3 1,growx,aligny top");
		
		passwordField.addKeyListener( new KeyAdapter() {
	        public void keyPressed(KeyEvent ke) {
	        	if(ke.getKeyCode() == KeyEvent.VK_ENTER)
	        	{
	        		Boolean check = Login.CheckPassword(txtUsername.getText(), passwordField.getText());
					
					if(check == true){
						if(txtUsername.getText().equals("admin"))
							JOptionPane.showMessageDialog(null, "Welcome back, administrator.", "Notice", JOptionPane.INFORMATION_MESSAGE);
						home x = new home();
						x.setVisible(true);
						dispose();
					}
					else{
						JOptionPane.showMessageDialog(null, "Invalid login Information.", "Notice", JOptionPane.INFORMATION_MESSAGE);
					}
	        	}

	        }

	    });
		
		btnLogin = new JButton("Login");
		btnLogin.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				Boolean check = Login.CheckPassword(txtUsername.getText(), passwordField.getText());
				if(Login.LoggedInUser.BanEndDate != null && Login.LoggedInUser.BanEndDate.getDate() > new Date().getDate()){
					JOptionPane.showMessageDialog(null, "You are currently not allowed to access the forum.", "Notice", JOptionPane.INFORMATION_MESSAGE);
					check = false;
					login x = new login();
					x.setVisible(true);
					dispose();
				}
				
				if(check == true){
					if(txtUsername.getText().equals("admin"))
						JOptionPane.showMessageDialog(null, "Welcome back, administrator.", "Notice", JOptionPane.INFORMATION_MESSAGE);
					home x = new home();
					x.setVisible(true);
					dispose();
				}
				else{
					JOptionPane.showMessageDialog(null, "Invalid login Information.", "Notice", JOptionPane.INFORMATION_MESSAGE);
				}
				
			}
		});
		contentPane.add(btnLogin, "cell 2 2,grow");
		
		btnRegister = new JButton("Register");
		btnRegister.addMouseListener(new MouseAdapter() {
			//switches to register.java (register a user JFrame)
			public void mousePressed(MouseEvent e) {
				register x = new register();
				x.setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnRegister, "cell 2 3,growx,aligny top");
		
		btnForgotPassemail = new JButton("Forgot pass/email");
		btnForgotPassemail.addMouseListener(new MouseAdapter() {
			//switches to forgot.java (forgot password JFrame)
			public void mousePressed(MouseEvent e) {
				forgot x = new forgot();
				x.setVisible(true);
				dispose();
			}
		});
		contentPane.add(btnForgotPassemail, "cell 2 4,growx,aligny top");
	}

}
